package com.py.pythink.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.py.pythink.bo.UserDetailAddBo;
import com.py.pythink.config.RedisKeyConfig;
import com.py.pythink.dao.OssLogDao;
import com.py.pythink.dao.UserDetailDao;
import com.py.pythink.entity.User;
import com.py.pythink.service.intf.OssLogService;
import com.py.pythink.service.intf.UserDetailService;
import com.py.pythink.third.JedisUtil;
import com.py.pythink.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
public class UserDetailServiceImpl implements UserDetailService {
    @Autowired
    private UserDetailDao dao;
    @Autowired
    private OssLogService service;
    // 初始化用户信息补全
    @Override
    public R init(UserDetailAddBo userDetailAddBo) {
        if(dao.update(userDetailAddBo)>0){
            return R.ok();
        }
        return R.fail("完善信息失败，请检查网络");
    }
    // 改变头像
    @Override
    @Transactional//开启事务
    public R changeHeader(int uid, int oid) {
        if(dao.updateByUid(oid,uid)>0){
            return R.ok();
        }
        return R.fail("完善信息失败，请检查网络");
    }
    @Override
    public R queryDetails(String token) {
        String key = RedisKeyConfig.USER_TOKEN+token;
        if(JedisUtil.checkKey(key)){
            User user = JSON.parseObject(JedisUtil.getStr(key), User.class);
            return R.ok(dao.selectByUids(user.getId()));
        }
        return R.fail();
    }

}
